package com.pro.controller;

import com.pro.pojo.JsActicleCategroy;
import com.pro.pojo.JsonData;
import com.pro.service.JsActicleCategroyService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:JsActicleCategroyController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2021/11/28 下午 6:33
 * @Author: WYH
 */
@Controller
@RequestMapping("/micro")
public class JsActicleCategroyController {

    @Autowired
    private JsActicleCategroyService jsActicleCategroyService;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @RequestMapping("/getAllzTree")
    @ResponseBody
    public JsonData getAllzTree(){
        List<JsActicleCategroy> allzTree = jsActicleCategroyService.getAllzTree();
        return allzTree != null ? JsonData.buildSuccess(allzTree) : JsonData.buildError("数据格式异常");
    }

    @RequestMapping("/addActicleCategroy")
    @ResponseBody
    public JsonData addActicleCategroy(JsActicleCategroy jsActicleCategroy) {
        Boolean aBoolean = jsActicleCategroyService.addActicleCategroy(jsActicleCategroy);
        return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("数据格式异常");
    }

    @RequestMapping("/delActicleCategroy")
    @ResponseBody
    public JsonData delActicleCategroy(Integer acticleCategroyId){
        Boolean aBoolean = jsActicleCategroyService.delActicleCategroy(acticleCategroyId);
        return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("数据格式异常");
    }

    @RequestMapping("/updateActicleCategroy")
    @ResponseBody
    public JsonData updateActicleCategroy(JsActicleCategroy jsActicleCategroy){
        Boolean aBoolean = jsActicleCategroyService.updateActicleCategroy(jsActicleCategroy);
        return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("数据格式异常");
    }

    @RequestMapping(value = "/layeditUpload")
    @ResponseBody
    public JsonData layeditUpload(@RequestParam("file") MultipartFile file){
        Map<String, Object> map1 = new HashMap<>();
        String layeditUploadUrl="";
        try {
            String f = fastDFSClientUtil.uploadFile(file);
            layeditUploadUrl = fastDFSClientUtil.getResAccessUrl(f);

            map1.put("src",layeditUploadUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map1 != null?JsonData.buildSuccess(map1):JsonData.buildError("文件上传失败");
    }
}
